package io.github.lee.core.ui.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.ViewDataBinding
import com.chad.library.adapter4.BaseQuickAdapter
import com.chad.library.adapter4.viewholder.DataBindingHolder

abstract class DataBindingAdapter<T : Any, VB : ViewDataBinding> :
    BaseQuickAdapter<T, DataBindingHolder<VB>>() {
    override fun onBindViewHolder(holder: DataBindingHolder<VB>, position: Int, item: T?) {
        if (null == item) {
            return
        }
        onItemHolder(holder.binding, item)
    }

    override fun onCreateViewHolder(
        context: Context,
        parent: ViewGroup,
        viewType: Int
    ): DataBindingHolder<VB> =
        DataBindingHolder(
            onCreateDataBinding(
                context,
                LayoutInflater.from(context),
                parent,
                viewType
            )
        )

    protected abstract fun onCreateDataBinding(
        context: Context, inflater: LayoutInflater,
        parent: ViewGroup,
        viewType: Int
    ): VB

    protected abstract fun onItemHolder(binding: VB, item: T)


}